package com.atguigu.guli.service.ucenter.controller.api;


import com.atguigu.guli.service.base.dto.MemberDto;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.base.utils.JwtHelper;
import com.atguigu.guli.service.base.utils.JwtInfo;
import com.atguigu.guli.service.ucenter.entity.Member;
import com.atguigu.guli.service.ucenter.entity.form.MemberRegistForm;
import com.atguigu.guli.service.ucenter.service.MemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-08-10
 */
@RestController

@RequestMapping("/api/ucenter/member")
public class ApiMemberController {
    @Autowired
    MemberService memberService;

    //4.service-trade项目远程访问，根据用户id查询用户详情
    @GetMapping("getMemberDto/{id}")
    public R getMemberDto(@PathVariable String id){
        Member member = memberService.getById(id);
        MemberDto memberDto = new MemberDto();
        BeanUtils.copyProperties(member,memberDto);
        return R.ok().data("item",memberDto);
    }
    //3、解析jwtToken  将有效载荷的数据解析为jwtInfo对象的方法
    @GetMapping("/auth/getUserInfo")
    public R getUserInfo(HttpServletRequest request){//jwtToken需要通过请求报文的头提交，key时token
        JwtInfo jwtInfo = JwtHelper.getJwtInfo(request);
        return R.ok().data("item",jwtInfo);

    }
    //2.认证方法：登陆成功将用户信息封装为jwt token响应【前端需要缓存】   构建
    @PostMapping("login")
    public R login(@RequestParam("mobile") String mobile,
                    @RequestParam("password") String password){
        String jwtToken = memberService.login(mobile,password);
        return R.ok().data("jwt",jwtToken);
    }
    //个人中心 ：登录  注册  修改个人信息
    //1、注册请求
    @PostMapping("regist")
    public R regist(@RequestBody MemberRegistForm member){
        //注册请求
        memberService.regist(member);
        return R.ok();
    }
}

